<?
include_once "../config/constants.php";

//initiate
	$cmd = "ffmpeg -y -i $SourceMedia $EncRule";
	$rre = null;
	$data = null;
	
	//function Encode( $cmd, $wsclient ){
	//$cmd = "ffmpeg -y -i c.flv tmp.flv";
	//$cmd = "ffmpeg -y -i b.flv bb.flv";

	$fp = popen("$cmd 2>&1", "r"); 
	$str_fp = "".$fp;
	$prog = 0;
	echo "\n---------------------------------------------------\n\n";
	echo "Encoding Process($str_fp) : $i\n";

	while(!feof($fp)) 
	{ 
		usleep(ENC_STATUS_REFRESH_TIME);
		$data = $data.fread($fp, 2048); 

		switch($enc){
			case '-':
				$enc = '\\'; break;
			case '\\':
				$enc = '|'; break;
			case '|':
				$enc = '/'; break;
			default:
				$enc = '-'; break;
		}
		//echo "\r\t\t\t\tEncoding";
		//for( $tmp = 0 ; $tmp < 10 ; $tmp++ )
		//	if( $prog / 5 >= $tmp ) echo ".";
		//	else echo " ";
		//echo "$enc";

		if( !$rre ){
			if( preg_match_all("([0-9]+(\.[0-9]+)?\sfps)", $data, $m1) != 2 ||
				!preg_match_all("(Duration:\s[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9])", $data, $m2) ) continue;
			$rre = -1;
			$r1 = sscanf($m1[0][1], "%f");
			$r2 = sscanf($m2[0][0], "Duration: %d:%d:%f");
			$full = ($r2[0] * 3600 + $r2[1] * 60 + $r2[2])*$r1[0];
		}
		else if( ($re = strrpos( $data, "frame" )) && $re != $rre ){
			sscanf(substr($data, $re), "frame= %d", $fn);
			if( $fn <= $full && (int)($fn / $full * 50) != $prog ){
				$prog = (int)($fn / $full * 50);
				//$result = $wsclient->updateContentStatus( array('_cid'=>$i, '_status'=>220+$prog));
/*	            $reqc = new HttpRequest(SRV_CONTENT_STATUS_UPDATE, HttpRequest::METH_POST);
	            $reqc->addPostFields(array('_cid'=>$i));
    	        $reqc->addPostFields(array('_status'=>(200+$prog)));
	            $reqc->send();
*/				$_cid = $i;
				$_status = (220+$prog);
				include "../ServiceManager/UpdateContentStatus.php";
			}	
			$rre = $re;	
		}
	} 
	
	$p_ret = pclose($fp);
	echo "p_ret($str_fp): $p_ret\n";


	if( ($re = strrpos( $data, "Lsize" ) ) ){

		$T = MEDIA_WMS_URI;
		$ext = substr($EncRule, strrpos($EncRule, ".")-strlen($EncRule));
		echo "\r\t\t\t\tEncoding Complete($str_fp)                      ";
		sscanf( substr( $data, $re ), "Lsize= %d%s time=%f bitrate=%f", $filesize, $g, $duration, $brate );
		//$result = $wsclient->updateContentStatus( array('_cid'=>$i, '_status'=>STATUS_CONTENTS_ENCODED));
/*        $reqc = new HttpRequest(SRV_CONTENT_STATUS_UPDATE, HttpRequest::METH_POST);
        $reqc->addPostFields(array('_cid'=>$i));
        $reqc->addPostFields(array('_status'=>STATUS_CONTENTS_ENCODED));
        $reqc->send();
*/		$_cid=$i;
		$_status=STATUS_CONTENTS_ENCODED;
		include "../ServiceManager/UpdateContentStatus.php";

		$query = 	"UPDATE  `contentsmetadata_db`.`tblLocation` 
					SET  `location` = '$T/$i$ext'
					WHERE `tblLocation`.`tblContent_tblCID_cid` = '$i'";
        mysql_query( $query );
		$query = 	"UPDATE `contentsmetadata_db`.`tblContent`
					SET `dc_identifier` = '$T/$i$ext', `xmpdm_duration` = '$duration',  `canssvideo_filesize` = '$filesize'
					WHERE `tblContent`.`tblCID_cid` = '$i'";
		mysql_query( $query );

		//force update state -> 310
		$query = "UPDATE `contentsmetadata_db`.`tblContentDescription`
				 SET `tblContentDescription`.`cansscontentdescription_status`=".STATUS_CONTENTS_OPUBLISHED." 
				 WHERE `tblContentDescription`.`cansscontentdescription_status`=".STATUS_CONTENTS_ENCODED." 
				  AND `tblContentDescription`.`tblCID_cid`='$i' 
				  AND `tblContentDescription`.`tblPID_pid` IN 
				   (SELECT `tblPerson`.`tblPID_pid` FROM `contentsmetadata_db`.`tblPerson` 
				     WHERE `tblPerson`.`canssperson_type`=40 )";
		mysql_query( $query );
	}
	else{
		echo "\r\t\t\t\tEncoding fail($str_fp)                      ";
		$data = trim($data);
		$errmsg = substr( $data, strrpos($data, "\n")-strlen($data) ) ;
		echo "\n$cmd\nErrMsg : $errmsg";
		//$result = $wsclient->updateContentStatus( array('_cid'=>$i, '_status'=>STATUS_CONTENTS_ENC_FAIL));
		//echo "\nUpdateStatus: $result\n";
/*        $reqc = new HttpRequest(SRV_CONTENT_STATUS_UPDATE, HttpRequest::METH_POST);
        $reqc->addPostFields(array('_cid'=>$i));
        $reqc->addPostFields(array('_status'=>STATUS_CONTENTS_ENC_FAIL));
        $bodyc = $reqc->send()->getBody();
		echo "\nUpdateStatus: $bodyc\n";
*/		$_cid=$i;
		$_status=STATUS_CONTENTS_ENC_FAIL;
		include "../ServiceManager/UpdateContentStatus.php";


		exit;
	}
	//}
	echo "\n";
?>
